package com.xx.pmis.persistence.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;

import com.xx.pmis.model.Wine;

public class WineDAOImpl extends BaseHibernateDAOImpl<Wine> implements WineDAO {

	public Wine getWineByWineID(String wineID) {
		Wine wine;
		try {
			wine = (Wine) getSession().get(Wine.class, wineID);

		} catch (HibernateException ex) {
			throw convertHibernateAccessException(ex);
		}
		return wine;
	}

	@SuppressWarnings("unchecked")
	public List<Wine> getAllWineByCondition(String hql) {
		List<Wine> wines;
		try {
			Query query = getSession().createQuery("from Wine" + hql);
			wines = query.list();
		} catch (HibernateException ex) {
			throw convertHibernateAccessException(ex);
		}
		return wines;
	}

	@SuppressWarnings("unchecked")
	public List<Wine> getAllWineByConditionByPage(String hql, int currentPage, int lineSize) {
		List<Wine> wines;
		try {
			Query query = getSession().createQuery("from Wine" + hql);
			query.setFirstResult((currentPage - 1) * lineSize);
			query.setMaxResults(lineSize);
			wines = query.list();
		} catch (HibernateException ex) {
			throw convertHibernateAccessException(ex);
		}
		return wines;
	}


}
